home *** CD-ROM | disk | FTP | other *** search
/ Macmillan Math: Grade 1 …Pupil's Edition (Florida) / Math Grade 1 - Pupil's Edition (Florida).iso / pc / corsair / generic / javascript / leftlink_treemenu.js < prev    next >
Encoding:
Text File  |  2003-05-29  |  9.2 KB  |  435 lines

  1. // filename        : treeMenu.js
  2. // description    : this js file contains functions to create a tree type
  3. //                : menu in html
  4.  
  5. Position = 0;            //stores the position to place the layer
  6. Level=0;                //stores at which level it is
  7. LevelPos=0;                //stores the position at which the level is
  8. Found=false;            //stores if the layer to be open is found
  9. imgColl = new Array();    //stores the layers which r opened
  10. closedImg = new Image();
  11. openImg = new Image();
  12.  
  13.  
  14. //this function creates a tree
  15. //parameters:
  16. //        x        -    x pos to build the menu
  17. //        y        -    y pos to build the menu
  18. //        width    -    width of the layer
  19. //
  20. function TreeMenu(x,y,width) 
  21. {
  22.     this.x = x;
  23.     this.y = y;
  24.     this.width = width;
  25.     this.style = "move";
  26.     
  27.     this.lists = new Array();            // sublists
  28.     this.items = new Array();            // layers
  29.     this.types = new Array();            // type
  30.  
  31.     this.setStyle = setStyle            //sets the style of the tree menu
  32.     this.setImage = setImage            //sets the open and close graphics
  33.     this.addItem = addItem;                //adds an item to the tree
  34.     this.addList = addList;                //adds a sublist to the tree
  35.     this.build = build;                    //builds the menu
  36.     this.switchIt = switchOpenClose;    //opens or closes the menu
  37.     this.switchMoveMenu = switchMenu    //opens or closes submenu in move style
  38.     this.switchCloseMenu = switchToggle    //opens or close submenu in openclose style
  39.     this.open = showSubMenu                //opens the menu
  40.     this.close = closeSubMenu            //closes the menu
  41.     this.closeAll = closeSubMenuToggle//closes the menu
  42.     this.switchImage = switchImage        //switches the image
  43.     this.closeAllImages = closeImages    //sets all images to default in that tree.
  44. }
  45.  
  46.  
  47. //this function sets the images to indicate open or close
  48. //parameters:
  49. //        openimage    -    the image to be displayed when the menu is open.
  50. //        closedimage    -    the image to be displayed when the menu is closed.
  51. //
  52. function setImage(openimage,closedimage)
  53. {
  54.     closedImg.src = closedimage;
  55.     openImg.src = openimage;
  56. }
  57.  
  58.  
  59.  
  60.  
  61. //this function sets the style of the tree menu i.e move/openclose
  62. //parameters:
  63. //        style    -    type of tree menu
  64. //
  65. function setStyle(style)
  66. {
  67.     this.style = style
  68. }
  69.  
  70.  
  71.  
  72. //this function adds a layer to the root
  73. //parameters:
  74. //        layername    -    nameof the div to pick up
  75. //
  76. function addItem(layername) 
  77. {
  78.     pos = this.items.length;
  79.  
  80.     //create an object
  81.     this.items[pos] = new Object();
  82.     this.types[pos] = "item";
  83.  
  84.     //set the properties for that object
  85.     this.items[pos].open = false;
  86.     this.items[pos].layername = layername;
  87.  
  88.     this.items[pos].link = new DynLayer(layername);
  89.     this.items[pos].linkHeight = this.items[pos].link.getContentHeight();
  90.     
  91.     // Calculate The Postion For Each layer
  92.     if (pos == 0) 
  93.     {
  94.         this.items[pos].nextLink = this.y
  95.     }
  96.     else 
  97.     {
  98.         this.items[pos].nextLink = this.items[pos-1].nextLink + this.items[pos-1].linkHeight
  99.     }
  100.  
  101. }
  102.  
  103.  
  104. //this function adds a layer to the root
  105. //parameters:
  106. //        list        -    the sublist
  107. //        layername    -    nameof the div to pick up
  108. //
  109. function addList(list,layername) 
  110. {
  111.     pos = this.items.length;
  112.  
  113.     //create an object
  114.     this.lists[pos] = list;
  115.     this.items[pos] = new Object();
  116.     this.types[pos] = "list";
  117.     
  118.  
  119.     //set the properties for that object
  120.     this.items[pos].open = false
  121.     this.items[pos].layername = layername;
  122.  
  123.     this.items[pos].link = new DynLayer(layername);
  124.     this.items[pos].linkHeight = this.items[pos].link.getContentHeight();
  125.     
  126.     // Calculate The Postion For Each layer
  127.     if (pos == 0) 
  128.     {
  129.         this.items[pos].nextLink = this.y
  130.     }
  131.     else 
  132.     {
  133.         this.items[pos].nextLink = this.items[pos-1].nextLink + this.items[pos-1].linkHeight
  134.     }
  135.  
  136. }
  137.  
  138.  
  139.  
  140. //this function builds all the layers
  141. //parameters:
  142. //
  143. function build() 
  144. {
  145.     // Initialize totalHeightOfLayers To 0
  146.     numBlocks = this.items.length;
  147.     totalHeightOfLayers = 0;
  148.     
  149.     for (var i=0;i<numBlocks;i++) 
  150.     {
  151.         // Calculate Total Height Of All Layers
  152.         totalHeightOfLayers += this.items[i].linkHeight;
  153.  
  154.         // Move Each layerMenuLink Into Position
  155.         if (i == 0) 
  156.         {
  157.             this.items[i].link.moveTo(this.x,this.y);
  158.         }
  159.         else 
  160.         {
  161.             this.items[i].link.moveTo(this.x,this.items[i].nextLink);
  162.         }
  163.         
  164.         // Show Each layerMenuLink After They Are Moved Into Place
  165.         this.items[i].link.show();
  166.  
  167.     }
  168.  
  169.     // Add totalHeightOfLayers To layerMenuBuffer To Deal With IE On Mac Problems
  170.     if (is.ie) 
  171.         document.all.layerMenuBuffer.style.height = totalHeightOfLayers;
  172. }
  173.  
  174.  
  175.  
  176. //this function opens or closes the menu
  177. //parameters:
  178. //        layernum        - name of the level to open
  179. //
  180. function switchOpenClose(layernum) 
  181. {
  182.     // Set Position Initially To Y Offset
  183.     Position = this.y
  184.     Level=0;
  185.  
  186.     //change the menu
  187.     if(this.style == "move")
  188.         this.switchMoveMenu('layer'+layernum);
  189.     else
  190.     {
  191.         //window.scroll(0,0);
  192.         this.switchCloseMenu('layer'+layernum);
  193.     }
  194.  
  195.     //change the image
  196.     this.switchImage(layernum);
  197.  
  198.     // Adjust Netscapes Document Size To Size Of Visible Layers
  199.     // if (is.ns4) document.height = Position+this.items[0].linkHeight
  200.  
  201.     
  202.  
  203.  
  204.  
  205.  
  206. function switchImage(layernum)
  207. {
  208.  
  209.     if (is.ns4)
  210.     {
  211.         if(eval('document.layer'+layernum+'.document.images["img'+layernum+'"]').src == openImg.src)
  212.         {
  213.             eval('document.layer'+layernum+'.document.images["img'+layernum+'"]').src = closedImg.src;
  214.         }
  215.         else
  216.         {
  217.             eval('document.layer'+layernum+'.document.images["img'+layernum+'"]').src = openImg.src;
  218.         }
  219.  
  220.     }
  221.     else
  222.     {
  223.         if(eval('document.images.img'+layernum).src == openImg.src)
  224.             eval('document.images.img'+layernum).src = closedImg.src;
  225.         else
  226.             eval('document.images.img'+layernum).src = openImg.src;        
  227.     }
  228. }
  229.  
  230.  
  231.  
  232. function closeImages()
  233. {
  234.     var layer;
  235.     for(var i=0; i<imgColl.length; i++)
  236.     {
  237.         layer = imgColl[i];
  238.         this.switchImage(layer.substring(5,layer.length));
  239.     }
  240. }
  241.  
  242.  
  243.  
  244. function switchToggle(layername) 
  245. {
  246.     var numBlocks = this.items.length;
  247.     var i = 0;
  248.     for (i=0; i<numBlocks; i++ )
  249.     {
  250.         //check if its a list
  251.         if(this.types[i] == "list")
  252.         {            
  253.             //check if this is the layer
  254.             if(this.items[i].layername == layername)
  255.             {            
  256.                 //show this link
  257.                 this.items[i].link.moveTo(this.x,Position);
  258.                 Position = Position + this.items[i].linkHeight;
  259.                 Found = true;
  260.                 if(this.items[i].open == false)
  261.                 {
  262.                     //open the layer if it is closed
  263.                     this.items[i].open = true;                    
  264.                     this.open(i);                        
  265.                 }
  266.                 else
  267.                 {
  268.                     //close the layer if it is open
  269.                     this.items[i].open = false;
  270.                     imgColl = new Array();                        
  271.                     this.closeAll(i);
  272.                     this.closeAllImages();
  273.                 }
  274.             }
  275.             else
  276.             {
  277.                 this.items[i].link.moveTo(this.x,Position);
  278.                 Position = Position + this.items[i].linkHeight;
  279.                 if(Level == 0)
  280.                     LevelPos = Position;
  281.                 //check if it is open
  282.                 if(this.items[i].open == true)
  283.                 {    
  284.                     Level=Level+1;
  285.                     this.lists[i].switchCloseMenu(layername);
  286.                     Level=Level-1;
  287.                 }
  288.             }
  289.  
  290.             if(Level==0)
  291.             {
  292.                 if(Found == false)
  293.                 {
  294.                     if(this.items[i].open == true)
  295.                     {
  296.                         this.items[i].open = false;
  297.                         imgColl = new Array();
  298.                         imgColl[imgColl.length] = this.items[i].layername;
  299.                         this.closeAll(i);
  300.                         this.closeAllImages();
  301.                         this.switchImage(i);
  302.                         Position = LevelPos;
  303.                     }
  304.                 }
  305.                 
  306.                 Found = false;
  307.             }
  308.             
  309.         }
  310.         else
  311.         {
  312.             this.items[i].link.moveTo(this.x,Position);
  313.             Position = Position + this.items[i].linkHeight;
  314.         }
  315.         
  316.     }
  317.     
  318.  
  319.  
  320.  
  321.  
  322.  
  323. function switchMenu(layername) 
  324. {
  325.     var numBlocks = this.items.length;
  326.     var i = 0;
  327.     for (i=0; i<numBlocks; i++ )
  328.     {
  329.         //check if its a list
  330.         if(this.types[i] == "list")
  331.         {            
  332.             //check if this is the layer
  333.             if(this.items[i].layername == layername)
  334.             {            
  335.                 //show this link
  336.                 this.items[i].link.moveTo(this.x,Position);
  337.                 Position = Position + this.items[i].linkHeight;
  338.  
  339.                 if(this.items[i].open == false)
  340.                 {
  341.                     //open the layer if it is closed
  342.                     this.items[i].open = true;
  343.                     this.open(i);                        
  344.                 }
  345.                 else
  346.                 {
  347.                     //close the layer if it is open
  348.                     this.items[i].open = false;
  349.                     this.close(i);
  350.                 }
  351.             }
  352.             else
  353.             {
  354.                 this.items[i].link.moveTo(this.x,Position);
  355.                 Position = Position + this.items[i].linkHeight;
  356.  
  357.                 //check if it is open
  358.                 if(this.items[i].open == true)
  359.                 {    
  360.                     this.lists[i].switchMoveMenu(layername);                    
  361.                 }
  362.             }
  363.             
  364.         }
  365.         else
  366.         {
  367.             this.items[i].link.moveTo(this.x,Position);
  368.             Position = Position + this.items[i].linkHeight;
  369.         }
  370.         
  371.     }
  372.     
  373.  
  374.  
  375. //this function opens the submenu
  376. //parameter:
  377. //        i    -    current level
  378. function showSubMenu(i) 
  379. {
  380.     numBlocks = this.lists[i].items.length;
  381.     
  382.     for (subBlocks=0;subBlocks<numBlocks ; subBlocks++)
  383.     {        
  384.         this.lists[i].items[subBlocks].link.moveTo(this.x,Position)
  385.         Position = Position + this.lists[i].items[subBlocks].linkHeight;
  386.         this.lists[i].items[subBlocks].link.show();
  387.     }
  388.                     
  389.  
  390.  
  391. //this function closes the submenu
  392. //parameter:
  393. //        i    -    current level
  394. function closeSubMenu(i) 
  395. {
  396.     var numBlocks = this.lists[i].items.length;
  397.     var subBlocks=0;
  398.  
  399.     for (subBlocks=0;subBlocks<numBlocks ; subBlocks++)
  400.     {    
  401.         if(this.lists[i].types[subBlocks] == "list")
  402.             this.lists[i].close(subBlocks);
  403.         this.lists[i].items[subBlocks].link.hide();
  404.     }
  405.  
  406.  
  407. //this function closes the submenu in toggle mode
  408. //parameter:
  409. //        i    -    current level
  410. function closeSubMenuToggle(i) 
  411. {
  412.     var numBlocks = this.lists[i].items.length;
  413.     var subBlocks=0;
  414.  
  415.     for (subBlocks=0;subBlocks<numBlocks ; subBlocks++)
  416.     {    
  417.         if(this.lists[i].types[subBlocks] == "list")
  418.         {    
  419.             if(this.lists[i].items[subBlocks].open == true)
  420.             {
  421.                 this.lists[i].items[subBlocks].open = false;
  422.                 imgColl[imgColl.length] = this.lists[i].items[subBlocks].layername;
  423.                 this.lists[i].closeAll(subBlocks);
  424.             }
  425.         }
  426.         this.lists[i].items[subBlocks].link.hide();
  427.         
  428.     }
  429.